﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
using CommonClasses;

namespace QCCM2
{
    public class QCAttrib
    {
        public static string ConnectionString()
        {
            string connectionString = @"Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
            return connectionString;
        }

        private int _ID;
        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private string _Desc;
        public string Desc
        {
            get { return _Desc; }
            set { _Desc = value; }
        }

        private string datatype;
        public string TypeOfData
        {
            get { return datatype; }
            set { datatype = value; }
        }

        private bool _Disabled;
        public bool Disabled
        {
            get { return _Disabled; }
            set { _Disabled = value; }
        }

        Log LogUpdate = new Log();


        public List<QCAttrib> GetAttribs()
        {
            string QueryString = "Select * FROM dbo.QCAttrib";
            DataTable dt = new DataTable();
            
            List<QCAttrib> QCAttribList = new List<QCAttrib>();

            using (SqlDataAdapter da = new SqlDataAdapter(QueryString, ConnectionString()))
            {
                da.Fill(dt);
                foreach (DataRow row in dt.Rows)
                {
                    QCAttrib qca = new QCAttrib();
                    qca.ID = Convert.ToInt32(row["QCAttribID"]);
                    qca.Name = Convert.ToString(row["QCAttribName"]);
                    qca.TypeOfData = Convert.ToString(row["DataType"]);
                    qca.Desc = Convert.ToString(row["QCAttribDescription"]);
                    qca.Disabled = Convert.ToBoolean(row["QCAttribDisabled"]);

                    QCAttribList.Add(qca);

                }
                
                return QCAttribList;
            }
        }

        public string AddQCAttrib(string Name, string TypeOfData, string Desc, bool Disabled)
        {
            string result = "";

            //inserts row into user table
            SqlConnection connection = new SqlConnection(ConnectionString());

            try
            {
                using (SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO dbo.QCAttrib VALUES(@QCAttribName, @DataType, @QCAttribDescription, @QCAttribDisabled)", connection))
                {
                    insertCommand.Parameters.AddWithValue("@QCAttribName", Name);
                    insertCommand.Parameters.AddWithValue("@DataType", TypeOfData);
                    insertCommand.Parameters.AddWithValue("@QCAttribDescription", Desc); 
                    insertCommand.Parameters.AddWithValue("@QCAttribDisabled", Disabled);

                    insertCommand.Connection = connection;
                    connection.Open();
                    insertCommand.ExecuteNonQuery();
                    result = "Success! QC Attribute\r" + Name + " has been added.";
                }

            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }

            LogUpdate.LogTransaction(0, Name + " has been Successfully Added", "", "Info");
            return result;
        }// insert row in group table


        public string UpdateQCAttrib(string Name, string TypeOfData, string Desc, bool Disabled)
        {
            string result = "";
        
            try
            {
                SqlConnection cs = new SqlConnection("Data Source=t-sql;Initial Catalog=QCConfigurationManager;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False");
                string Query = "UPDATE dbo.QCAttrib SET QCAttribName = @QCAttribName, DataType = @DataType, [QCAttribDescription] = @QCAttribDescription, QCAttribDisabled = @QCAttribDisabled Where QCAttribName ='" + Name + "'";
                SqlCommand cm = new SqlCommand(Query, cs);

                cs.Open();

                cm.Parameters.AddWithValue("@QCAttribName", Name);
                cm.Parameters.AddWithValue("@DataType", TypeOfData);
                cm.Parameters.AddWithValue("@QCAttribDescription", Desc);
                cm.Parameters.AddWithValue("@QCAttribDisabled", Disabled);

                cm.ExecuteNonQuery();

                result = Name + " has been updated.";
                cs.Close();
            }
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = "Houston, we have a problem:\r" + sqlException.Message;
            }
            catch (Exception ex)
            {
                result = "There was a problem. Your changes many not be saved.\r" + ex.Message;
            }

            LogUpdate.LogTransaction(0, Name + " has been Successfully Updated", "", "Info");
            return result;
        }


        public string DeleteQCAttrib(string Name)
        {
            string result = "";
            SqlConnection connection = new SqlConnection(ConnectionString());

            try
            {
                using (SqlCommand deleteCommand = new SqlCommand(
                "DELETE FROM dbo.QCAttrib WHERE QCAttribName ='" + Name + "'", connection))
                {
                    deleteCommand.Connection = connection;
                    connection.Open();
                    deleteCommand.ExecuteNonQuery();
                    result = Name + " has been deleted.";
                }
            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }
            LogUpdate.LogTransaction(0, Name + " has been Successfully Deleted", "", "Info");
            return result;
        }


        public QCAttrib SearchQCAttrib(string Name)
        {
            string result = "";
            QCAttrib qca = new QCAttrib();
            SqlConnection connection = new SqlConnection(ConnectionString());
            SqlDataReader dr;

            try
            {
                using (SqlCommand selectCommand = new SqlCommand(
                "SELECT FROM dbo.QCAttrib WHERE QCAttribName ='" + Name + "'", connection))
                {
                    dr = selectCommand.ExecuteReader();
                    while (dr.Read())
                    {
                        qca.ID = Convert.ToInt32(dr["QCAttribID"]);
                        qca.Name = Convert.ToString(dr["QCAttribName"]);
                        qca.TypeOfData = Convert.ToString(dr["DataType"]);
                        qca.Desc = Convert.ToString(dr["QCAttribDescription"]);
                        qca.Disabled = Convert.ToBoolean(dr["QCAttribDisabled"]);
                    }
                }
            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                result = sqlException.Message;
            }
            LogUpdate.LogTransaction(0, Name + " Was Searched", "", "Info");
            return qca;
        }


        #region Liz's code
        //public string AddAttrib(int qcAttribId, string qcAttribName, string dataType, bool qcAttribDisabled, string qcAttribDescription)
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"
        //}

        //public string UpdateAttrib()
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"

        //}

        //public string DeleteAttrib()
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"

        //}

        //public string DisplayAttribs(int qcAttribId)
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"

        //}

        //public string DisableAttrib()
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"

        //}

        //public string EnableAttrib()
        //{
        //    string result = "";
        //    return result;
        //    //return string "success" or "not success"

        //}
        #endregion
    }
}
